package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.SmsFeeService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.sms.pojo.dto.SmsFeeChargeDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsFeeInfoDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsFeeLogDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsFeePayStatusDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsFeeQrCodeDTO;
import com.xbongbong.pro.sms.pojo.vo.SmsFeeChargeVO;
import com.xbongbong.pro.sms.pojo.vo.SmsFeeInfoVO;
import com.xbongbong.pro.sms.pojo.vo.SmsFeeLogVO;
import com.xbongbong.pro.sms.pojo.vo.SmsFeePayStatusVO;
import com.xbongbong.pro.sms.pojo.vo.SmsFeeQrCodeVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description:
 * @Author: hongxiao
 * @date: 2019-01-13 17:12
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/sms/fee")
public class SmsFeeController {

    @Resource
    private SmsFeeService smsFeeService;

    /**
     * 获取可充值的短信套餐信息
     *
     * @param br
     * @param smsFeeInfoDTO 入参
     * @return
     * @author hongxiao
     * @addTime: 2019-01-13 17:18
     * @modified by:
     * @updateTime: 2019-01-13 17:18
     * @version
     * @since
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getInfo(@RequestBody @Valid SmsFeeInfoDTO smsFeeInfoDTO, BindingResult br) {
        XbbResponse<SmsFeeInfoVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsFeeInfoVO smsFeeInfoVO = smsFeeService.getInfo(smsFeeInfoDTO);
                response = new XbbResponse<>(smsFeeInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 短信套餐充值
     *
     * @param br
     * @param smsFeeChargeDTO 入参
     * @return
     * @author hongxiao
     * @addTime: 2019-01-13 17:18
     * @modified by:
     * @updateTime: 2019-01-13 17:18
     * @version
     * @since
     */
    @RequestMapping(value = "/charge", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String charge(@RequestBody @Valid SmsFeeChargeDTO smsFeeChargeDTO, BindingResult br) {
        XbbResponse<SmsFeeChargeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsFeeChargeVO smsFeeChargeVO = smsFeeService.charge(smsFeeChargeDTO);
                response = new XbbResponse<>(smsFeeChargeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取短信套餐充值状态
     *
     * @param br
     * @param smsFeePayStatusDTO 入参
     * @return
     * @author hongxiao
     * @addTime: 2019-01-13 17:20
     * @modified by:
     * @updateTime: 2019-01-13 17:20
     * @version
     * @since
     */
    @RequestMapping(value = "/checkPayStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkPayStatus(@RequestBody @Valid SmsFeePayStatusDTO smsFeePayStatusDTO, BindingResult br) {
        XbbResponse<SmsFeePayStatusVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsFeePayStatusVO smsFeePayStatusVO = smsFeeService.checkPayStatus(smsFeePayStatusDTO);
                response = new XbbResponse<>(smsFeePayStatusVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取短信充值二维码
     *
     * @param br
     * @param smsFeeQrCodeDTO 入参
     * @return
     * @author hongxiao
     * @addTime: 2019-01-13 17:25
     * @modified by:
     * @updateTime: 2019-01-13 17:25
     * @version
     * @since
     */
    @RequestMapping(value = "/getQRCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getQRCode(@RequestBody @Valid SmsFeeQrCodeDTO smsFeeQrCodeDTO, BindingResult br) {
        XbbResponse<SmsFeeQrCodeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsFeeQrCodeVO smsFeeQrCodeVO = smsFeeService.getQRCode(smsFeeQrCodeDTO);
                response = new XbbResponse<>(smsFeeQrCodeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 获取充值记录列表
     *
     * @param br
     * @param smsFeeLogDTO 入参
     * @return
     * @author hongxiao
     * @addTime: 2019-01-13 17:27
     * @modified by:
     * @updateTime: 2019-01-13 17:27
     * @version
     * @since
     */
    @RequestMapping(value = "/log/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String logList(@RequestBody @Valid SmsFeeLogDTO smsFeeLogDTO, BindingResult br) {
        XbbResponse<SmsFeeLogVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsFeeLogVO smsFeeLogVO = smsFeeService.logList(smsFeeLogDTO);
                response = new XbbResponse<>(smsFeeLogVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
